package model;

import jade.core.AID;
import jade.util.leap.Serializable;

public class Offer implements Serializable, Comparable<Object> {
	
	private static final long serialVersionUID = -8671306618370368333L;
	public int price;
	public AID buyer;
	
	public Offer() {
		this.price = 0;
		this.buyer = null;
	}
	
	public String toString() {
		String buyerName = buyer != null ? this.buyer.getLocalName() : "null";
		return buyerName + " wants to buy the artifact for " + this.price + ":-";
	}
	
	public int getPrice() {
		return price;
	}
	
	@Override
	public int compareTo(Object obj) {
		try {
			Offer off = (Offer) obj;
			System.out.println("COMPARISON: Offer1: " + this);
			System.out.println("COMPARISON: Offer2: " + off);
			
			boolean thisWasNoOffer = this.buyer == null;
			boolean objWasNoOffer = off.buyer == null;
			boolean thisWasHigher = this.price > off.price;
			boolean objWasHigher = this.price < off.price;
			
			if(thisWasHigher && !thisWasNoOffer && !objWasNoOffer)
				return 1;
			if(objWasHigher && !thisWasNoOffer && !objWasNoOffer)
				return -1;
			if(thisWasNoOffer && !objWasNoOffer)
				return -1;
			if(!thisWasNoOffer && objWasNoOffer)
				return 1;
			else
				return 0;
			
		} catch (ClassCastException e) {
			System.out.println("These cannot be compared");
			e.printStackTrace();
			return 0;
		}
	}
}
